package com.jindun.paper.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jindun.paper.dao.ExclusionLimitationRepository;
import com.jindun.paper.dao.ExclusionThresholdRepository;
import com.jindun.paper.model.ExclusionLimitation;
import com.jindun.paper.model.ExclusionThreshold;
import com.jindun.paper.service.ExclusionService;

@Service
@Transactional
public class ExclusionServiceImpl implements ExclusionService {

	@Autowired
	private ExclusionThresholdRepository exclusionThresholdRepository;
	
	@Autowired
	private ExclusionLimitationRepository exclusionLimitationRepository;

	@Override
	public List<ExclusionThreshold> findAllThredHolds() {
		return exclusionThresholdRepository.findAll();
	}

	@Override
	public void save(Map<Integer, Integer> thresHoldValueMap,  List<Integer []> limitatons) {
	
		this.exclusionThresholdRepository.deleteAll();	
		
		this.exclusionLimitationRepository.deleteAll();
		
		for (Map.Entry<Integer, Integer> entry : thresHoldValueMap.entrySet()) {	
			ExclusionThreshold threshold = new ExclusionThreshold();
			threshold.setStubSpec(entry.getKey());
			threshold.setThreshold(entry.getValue());
			this.exclusionThresholdRepository.save(threshold);
		}
		
		for (Integer [] limit : limitatons) {
			if (limit [0] != null && limit [1] != null  && limit [2] != null) {
				ExclusionLimitation exclusionLimitation = new ExclusionLimitation();
				exclusionLimitation.setRangeStart(limit[0]);
				exclusionLimitation.setRangeEnd( limit[1]);
				exclusionLimitation.setMaxExclusion(limit[2]);
				this.exclusionLimitationRepository.save(exclusionLimitation);
				
			}
			
		}
		
		
		
	}

	@Override
	public List<ExclusionLimitation> findAllExclusionLimitations() {
		// TODO Auto-generated method stub
		return exclusionLimitationRepository.findAll();
	}
	
	
	
	
	
	

	
	
}
